//
//  ManageTieZiViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/2.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ManageTieZiViewController.h"
#import "ManageTieZiHeaderView.h"
#import "ManageTieZiTableViewCell.h"
#import "ManageTieZiModel.h"

@interface ManageTieZiViewController ()<UITableViewDelegate,UITableViewDataSource,ManageTieZiCellDalegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<ManageTieZiModel *> * managerListArray;
@property (nonatomic, copy)NSString * type;

@end

@implementation ManageTieZiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.type = @"0";
    
    [ManageTieZiModel manageTieZiWithType:self.type WithSucess:^(NSArray<ManageTieZiModel *> *array) {
        
        self.managerListArray = array;
    } error:^{
        
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReLoadDataForTableview) name:@"ReLoadDataForTableview" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeTableViewData:) name:@"ChangeTableViewData" object:nil];
    
    [self setupNav];
    [self setupUI];
}


- (void)setManagerListArray:(NSMutableArray<ManageTieZiModel *> *)managerListArray{
    
    _managerListArray = managerListArray;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"我的论坛";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 80) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 300 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[ManageTieZiTableViewCell class] forCellReuseIdentifier:@"ManageTieZiListCell"];
    [self.tableView registerClass:[ManageTieZiHeaderView class] forHeaderFooterViewReuseIdentifier:@"ManageTieZiHeaderView"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.managerListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ManageTieZiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ManageTieZiListCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.model = self.managerListArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40.5 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ManageTieZiHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ManageTieZiHeaderView"];
    
    return header;
}


- (void)ReLoadDataForTableview{
    
    [ManageTieZiModel manageTieZiWithType:self.type WithSucess:^(NSArray<ManageTieZiModel *> *array) {
        
        self.managerListArray = array;
    } error:^{
        
    }];
}


- (void)ChangeTableViewData:(NSNotification *)noti{

    self.type = noti.userInfo[@"type"];
    
    NSLog(@"type ==%@",self.type);
    
    [ManageTieZiModel manageTieZiWithType:self.type WithSucess:^(NSArray<ManageTieZiModel *> *array) {
        
        self.managerListArray = array;
    } error:^{
        
    }];
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark ManageTieZiCellDalegate
- (void)didCellNSString:(NSString *)string{

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:string
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              //                                                                          NSLog(@"action = %@", action);
                                                          }];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
